package com.kgc.basicprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.Good;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.dao.GoodMapper;
import com.kgc.basicprovider.service.GoodService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class GoodServiceImpl implements GoodService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private GoodMapper goodMapper;

    @Override
    public ResponseMsg getGoodById(String id) {
        logger.info("GoodServiceImpl getGoodById, id:{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        Good good = goodMapper.getGoodById(id);
        logger.debug("GoodServiceImpl getGoodById id:" + id + ", good:{}", good);
        if (good != null) {
            responseMsg.setData(good);
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getGoodPage(Map<String, Object> map) {
        logger.info("GoodServiceImpl getGoodPage, map:{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        PageHelper.startPage(Integer.valueOf(map.get("pageNow").toString()), Integer.valueOf(map.get("pageSize").toString()));
        List<Good> list = goodMapper.getAllGood(map);
        logger.debug("goodMapper getAllGood, list:{}", list + ", map:{}", map);
        PageHelper.clearPage();
        PageInfo<Good> pageInfo = new PageInfo<>();
        if (list != null && list.size() > 0) {
            pageInfo.setList(list);
            pageInfo.setPageNo(Integer.valueOf(map.get("pageNow").toString()));
            pageInfo.setPageSize(Integer.valueOf(map.get("pageSize").toString()));
            pageInfo.setTotal(goodMapper.getGoodCount(map));
        }
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            responseMsg.setCode("200");
            responseMsg.setData(pageInfo);
        } else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getAllGood(Map<String, Object> map) {
        logger.info("GoodServiceImpl getGoodPage, map:{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        List<Good> list = goodMapper.getAllGood(map);
        logger.debug("goodMapper getAllGood, list:{}", list + ", map:{}", map);
        if (list != null && list.size() > 0) {
            responseMsg.setCode("200");
            responseMsg.setData(list);
        } else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg delGood(String id) {
        logger.info("GoodServiceImpl delGood, id:{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("updateDate", new Date());
//        map.put("updateUser", "123");
        int count = goodMapper.delGood(map);
        logger.debug("goodMapper delGood, count:{}", count + ", id:{}", id);
        if (count > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg addGood(Good good) {
        logger.info("GoodServiceImpl addGood, good:{}", good);
        ResponseMsg responseMsg = new ResponseMsg();
        good.setCreateDate(new Date());
        good.setUpdateDate(new Date());
//        good.setCreateUser("1111");
//        good.setUpdateUser("1111");
        int count = goodMapper.addGood(good);
        logger.debug("goodMapper addGood, count:{}", count + ", good:{}", good);
        if (count > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg updateGood(Good good) {
        logger.info("GoodServiceImpl updateGood, good:{}", good);
        ResponseMsg responseMsg = new ResponseMsg();
        good.setUpdateDate(new Date());
//        good.setUpdateUser("222");
        int count = goodMapper.updateGood(good);
        logger.debug("goodMapper updateGood, count:{}", count + ", good:{}", good);
        if (count > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getGoodList(List<String> idList) {
        logger.info("GoodServiceImpl getGoodList, idList:{}", idList);
        ResponseMsg responseMsg = new ResponseMsg();
        List<Good> list = new ArrayList<Good>();
        for (String id : idList) {
            Good good = goodMapper.getGoodById(id);
            if (good != null) {
                list.add(good);
            }
        }
        logger.debug("goodMapper getGoodList, list:{}", list + ", idList:{}", idList);
        if (list != null && list.size() > 0) {
            responseMsg.setCode("200");
            responseMsg.setData(list);
        } else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg changeStock(Map<String, Object> map) {
        logger.info("GoodServiceImpl changeStock, map:{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        int count = goodMapper.changeStock(map);
        logger.debug("goodMapper changeStock, count:{}", count+", map:{}", map);
        if(count>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        return responseMsg;
    }
}
